/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ora_ttc7_tti_rxd.c                                 */
/*  NOTE      =                                                    */
/*  DATE      = 2014/11/21 by songbb                               */
/*******************************************************************/

#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>

#include "frcomm.h"
#include "frstr.h"
#include "frmem.h"
#include "frlog.h"

#include "conntrack.h"
#include "frct_comm.h"

#include "../../ora_core.h"
#include "../../comm/ora_comm.h"
#include "../../comm/ora_stmt.h"
#include "../../comm/ora_util.h"
#include "../ora_mar_engine.h"
#include "../accessor/ora_include_accessor.h"
#include "ora_ttc7_tti_rxh.h"
#include "ora_ttc7_item.h"


/*******************************************************************************
*  FUNC     :  解析具体的返回值内容
*  ARGS     :  *p_st_conn                           (ct信息)
*           :  *p_st_sess                           (Oracle私有的session)
*           :  *p_st_ora_packet                     (Oracle数据包)
*  RTN      :  0                                    (FR_SUCC)
*              1                                    (FR_FAIL)
*  NOTE     :
*******************************************************************************/
int ora_ttc7_tti_rxd_unmarshal(Conntrack *p_st_conn
                            , L7session_ora *p_st_sess
                            , L7_ora_packet *p_st_ora_packet)
{
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);
    L7_ora_curr_session *p_st_curr_sess = p_st_session_private->p_st_curr_sess;
    Sql_statement *p_st_stmt = NULL;
    Sql_metadata_info *p_st_metadata = NULL;
    Frlist_node *p_st_node = NULL;
    int i_loop = 0;
#if APM_ORA_DBG
    int i_pos_start = 0;
    int i_index = 1;
#endif

    if ((p_st_stmt = ora_get_current_stmt(p_st_conn)) == NULL)
        goto error;

    if (frlist_size(&(p_st_stmt->st_metadata_column)) > 0)
    {
        ORA_PRINT_DBG("get metadata from column list, size:[%d]\n", frlist_size(&(p_st_stmt->st_metadata_column)));
        p_st_node = FRLIST_FIRST_NODE(&(p_st_stmt->st_metadata_column));
    }
    else if (frlist_size(&(p_st_stmt->st_metadata_bind)) > 0 && frlist_size(&(p_st_stmt->st_metadata_define)) == 0)
    {
        ORA_PRINT_DBG("get metadata from bind list, size:[%d]\n", frlist_size(&(p_st_stmt->st_metadata_bind)));
        p_st_node = FRLIST_FIRST_NODE(&(p_st_stmt->st_metadata_bind));
    }
    else if (frlist_size(&(p_st_stmt->st_metadata_define)) > 0)
    {
        ORA_PRINT_DBG("get metadata from define list, size:[%d]\n", frlist_size(&(p_st_stmt->st_metadata_define)));
        p_st_node = FRLIST_FIRST_NODE(&(p_st_stmt->st_metadata_define));
    }
    else
    {
        ORA_PRINT_EMERG("Doesn't exists metadata\n");
        ORA_FRLOG_ERROR("Doesn't exists metadata");
        goto error;
    }

    while (p_st_node)
    {
        p_st_metadata = (Sql_metadata_info *) FRLIST_GET_NODE_DATA(p_st_node);
#if APM_ORA_DBG
        i_pos_start = bytebuff_getPosition(p_st_ora_packet->p_st_packet);
#endif
        ORA_PRINT_CRIT("p_st_metadata => index:[%d] i_data_type:[%d] i_isuseless:[%d] i_isnullbydescribe:[%d] oacflg:[%d]\n",
                i_index,
                p_st_metadata->i_data_type,
                p_st_metadata->i_isuseless,
                p_st_metadata->i_isnullbydescribe,
                p_st_metadata->s_ora_oacflg);

        ORA_PRINT_DTY_INFO((char *) "%s start\n", p_st_metadata->i_data_type);
        if (ora_ttc7_item_unmarshal_resp(p_st_conn, p_st_sess, p_st_curr_sess, p_st_ora_packet, p_st_metadata) != FR_SUCC)
            goto error;
        ORA_PRINT_DTY_INFO((char *) "%s done\n", p_st_metadata->i_data_type);

        i_loop++;
#if APM_ORA_DBG
        i_index++;
        ORA_PRINT_PACKET_SPECIFIED("unmarshaled one row", p_st_ora_packet, i_pos_start, bytebuff_getPosition(p_st_ora_packet->p_st_packet) - i_pos_start);
#endif
        p_st_node = FRLIST_NEXT_NODE(p_st_node);
    }

    return FR_SUCC;
error:
    return FR_FAIL;
}
